package com.cl.blog.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cl.blog.common.Const;
import com.cl.blog.entity.*;
import com.cl.blog.mapper.BCommunityMapper;
import com.cl.blog.mapper.BContentMapper;
import com.cl.blog.mapper.BUserMapper;
import com.cl.blog.service.BCommunityReviewService;
import com.cl.blog.service.BContentReviewService;
import com.cl.blog.mapper.BContentReviewMapper;
import com.cl.blog.util.JwtUtil;
import com.cl.blog.vo.ContentReviewVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author object
 * @description 针对表【b_content_review】的数据库操作Service实现
 * @createDate 2024-03-14 17:13:59
 */
@Service
public class BContentReviewServiceImpl extends ServiceImpl<BContentReviewMapper, BContentReview>
		implements BContentReviewService {

	@Autowired
	JwtUtil jwtUtil;
	@Autowired
	BCommunityReviewService bCommunityReviewService;
	@Autowired
	BContentReviewMapper bContentReviewMapper;
	@Autowired
	BContentMapper bContentMapper;
	@Autowired
	BCommunityMapper bCommunityMapper;
	@Autowired
	BUserMapper bUserMapper;

	@Override
	public Object selectAll(HttpServletRequest request) {
		String token = request.getHeader("token");
		Claims claim = jwtUtil.getClaimByToken(token, Const.GRADE_USER);
		// 文章评论的内容
		List<BContentReview> bContentReviews = this.list(new QueryWrapper<BContentReview>()
				.eq("user_id", Integer.valueOf(claim.getSubject())));
		for (BContentReview bContentReview : bContentReviews) {
			BContent bContent = bContentMapper.selectOne(new QueryWrapper<BContent>().eq("id", bContentReview.getContentId()));
			bContentReview.setBContent(bContent);
		}
		// 社区问答评论的内容
		List<BCommunityReview> bCommunityReviews = bCommunityReviewService.list(new QueryWrapper<BCommunityReview>()
				.eq("user_id", Integer.valueOf(claim.getSubject())));
		for (BCommunityReview bCommunityReview : bCommunityReviews) {
			BCommunity bCommunity = bCommunityMapper.selectOne(new QueryWrapper<BCommunity>().eq("id", bCommunityReview.getCommunityId()));
			bCommunityReview.setBCommunity(bCommunity);
		}

		return MapUtil.builder()
				.put("contentReview", bContentReviews)
				.put("communityReview", bCommunityReviews)
				.build();
	}

	@Override
	public Object selectContentReview(Integer contentId) {
		// 根据文章id查询评论(父级评论)
		List<BContentReview> bContentReviews = this.list(new QueryWrapper<BContentReview>()
				.eq("content_id", contentId)
				.isNull("parent_id")
				.orderByDesc("create_time")
		);

		for (BContentReview bContentReview : bContentReviews) {
			BUser bUser = bUserMapper.selectById(bContentReview.getUserId());
			bContentReview.setBUser(bUser);
			// 子级评论
			List<BContentReview> contentReviews = bContentReviewMapper.selectAllChildReview(contentId, bContentReview.getId());
			// 查询子级评论的用户数据
			for (BContentReview contentReview : contentReviews) {
				BUser bUser1 = bUserMapper.selectById(contentReview.getUserId());
				contentReview.setBUser(bUser1);
			}

			bContentReview.setList(contentReviews);

		}
		return bContentReviews;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean insertReview(ContentReviewVo reviewVo, HttpServletRequest request) {
		System.out.println(reviewVo);
		String token = request.getHeader("token");
		Claims claim = jwtUtil.getClaimByToken(token, Const.GRADE_USER);

		BContentReview bContentReview = new BContentReview();
		bContentReview.setContentId(reviewVo.getContentId());
		bContentReview.setReviewContent(reviewVo.getContent());
		bContentReview.setCreateTime(LocalDateTime.now());
		bContentReview.setUserId(Integer.valueOf(claim.getSubject()));
		if (reviewVo.getPrentId() != null) {
			bContentReview.setParentId(reviewVo.getPrentId());
		}
		bContentReview.setLikeCount(0L);

		return this.save(bContentReview);
	}
}




